<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############
















$submitted = false;
$error = false;

if(Antz_IntelliForm::submitted('template-new-upload')){
    $submitted = true;
    
    $filename = $_FILES['file']['tmp_name'];
    if(!file_exists($filename) || !is_readable($filename)){
        JFX::addError('No file attached, maybe too big?');
        $error = true;
        $_POST['code'] = '';
    }else{
        $_POST['code'] = file_get_contents($filename);
    }
    
}

if(Antz_IntelliForm::submitted('template-new-edit')){
    $submitted = true;
}

if($submitted && !$error){

    if(trim(post('title'))==''){
        $error = true;
        JFX::addError('Title is blank!');
    }

    if(trim(post('code')) == ''){
        $error = true;
        JFX::addError('No code given');
    }

    if(true === $this->addTemplate(post('title'), post('code', '', 5000))){
        JFX::addSuccess('Template successfully created!');
        JFX::redirect($CONFIG->adminUrl.'/templates/list/');
    }
}







// output begins
echo '<h1>Create new template</h1>
'.$this->lang('templates_intro');







$defaultCode = <<<CODE
<div id="my-new-div" class="droppable">

</div>
<style type="text/css">

</style>
<script type="text/javascript" language="javascript">

</script>
CODE;



/* found to be easier to just copy and paste from an editor
        $formFields = array(
            array(
                'type' => 'text',
                'name' => 'title',
                'value' => post('title'),
                'label' => 'Template Title'
            ),
            array(
                'type' => 'file',
                'name' => 'file',
                'value' => post('file'),
                'label' => 'Upload a file'
            ),
            array(
                'type' => 'submit',
                'name' => 'submitBtn',
                'value' => 'Submit',
                'label' => '&nbsp;'
            )
        );
        $form = JFX::makeRapidForm($formFields, 'Template upload', 'template-new-upload');
        echo (string) $form;
*/


        $formFields = array(
            array(
                'type' => 'text',
                'name' => 'title',
                'value' => post('title'),
                'label' => 'Template Title'
            ),
            array(
                'type' => 'code',
                'name' => 'code',
                'value' => post('code', $defaultCode, 5000),
                'class' => 'html',
                'id' => 'newtemplatecode',
                'label' => 'Code'
            ),
            array(
                'type' => 'submit',
                'name' => 'submitBtn',
                'value' => 'Create',
                'label' => '&nbsp;'
            )
        );
        $form = JFX::makeRapidForm($formFields, 'Template edit', 'template-new-edit');
        echo (string) $form;






